<template>
  <el-dialog
    :title="data.title"
    :visible.sync="data.visible"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    class="dialog-width"
  >
    <!-- el-form -->
    <el-form :model="data.form" :rules="rules" ref="form" label-width="100px" @submit.native.prevent>
      <el-form-item
        label="组名"
        prop="name"
      >
        <el-input
          placeholder="请输入"
          v-model="data.form.name"
        ></el-input>
      </el-form-item>
    </el-form>
    <!-- el-form -->
    <span slot="footer">
      <el-button @click="closeDialog">
        取消
      </el-button>
      <el-button
        type="primary"
        :loading="data.btnLoading"
        @click="submitDialog"
      >
        保存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as apis from '@/api/fairyshell'
export default {
  data() {
    return {
      data: {
        title: '新建分组',
        // 默认新增类型
        dialogType: 'add',
        btnLoading: false,
        node: null,
        visible: false,
        form: {
          // 名称
          name: '',
          parentId: 0 // 上级id
        },
        // 分组选项
        groups:[]
      },
      rules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    // 初始化
    init(data) {
      // form数据先重置
      this.clearDataForm()
      this.data.dialogType = data.flag
      this.data.node = data.value
      if (data.flag === 'edit-folder') {
        // 编辑
        this.data.title = '编辑分组'
        this.data.form.name = data.value.data.name
      } else {
        // 新增
        this.data.title = '新建分组'
      }
      this.data.visible = true
    },
    // 确定提交
    submitDialog() {
      this.$refs['form'].validate(valid => {
        this.$nextTick(() => {
          this.data.btnLoading = true
        })
        if (valid) {
          if (this.data.dialogType === 'add-folder') {
            // 新增
            this.addSaveFn()
          } else {
            // 编辑
            this.editSaveFn()
          }
        } else {
          this.$nextTick(() => {
            this.data.btnLoading = false
          })
        }
      })
    },
    // 更新element-ui 局部节点 废弃
    refreshLazyTree(node, children) {
      var theChildren = node.childNodes
      theChildren.splice(0, theChildren.length)
      node.doCreateChildren(children)
    },
    // 新增 => 保存
    addSaveFn() {
      const form = {...this.data.form,parentId: this.data.node && this.data.node.data && this.data.node.data.id ? this.data.node.data.id : '0' }
      apis
        .addFolder(form)
        .then((res) => {
          this.$message({
            type: 'success',
            message: '保存成功!'
          })
          console.log(res)
          // 更新列表
          this.$emit('updateList', this.data.node.data ? this.data.node.data : {})
        })
        .catch(res => {
          this.$notify.error({
            title: '错误',
            message: (res.data && res.data.mesg) || res.mesg
          })
        })
        .finally(() => {
          this.$nextTick(() => {
            this.data.btnLoading = false
            this.closeDialog()
          })
        })
    },
    // 编辑 => 保存
    editSaveFn() {
      const form = {...this.data.form,id: this.data.node.data.id,parentId: this.data.node.data.parentId}
      apis
        .updateFolder(form)
        .then(() => {
          this.$message({
            type: 'success',
            message: '更新成功!'
          })
          // 更新列表
          this.$emit('updateList', this.data.node.data)
        })
        .catch(res => {
          this.$notify.error({
            title: '错误',
            message: (res.data && res.data.mesg) || res.mesg
          })
        })
        .finally(() => {
          this.$nextTick(() => {
            this.data.btnLoading = false
            this.closeDialog()
          })
        })
    },
    clearDataForm() {
      // 清空数据
      Object.keys(this.data.form).forEach(key => (this.data.form[key] = ''))
    },
    // 取消
    closeDialog() {
      // 弹窗关闭 清除验证
      this.data.visible = false
      // 清空数据
      // this.clearDataForm()
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style lang="stylus" scoped></style>
